var authorityMenuGrid;

var authorityWiin = new Ext.Window({
		layout:'fit',
		width:600,
		height:400,
		closeAction:'hide',
		resizable:false,//true可以拖动大小.
		shadow : true,//投影效果.
		modal:true,//对后面内容进行遮盖.
		closable:true,
		bodyStyle:'padding:5 5 5 5',
		animCollapse:true//关闭面板是的动画效果.
});


function createAuthorityMenuGrid(){
	authorityMenuGrid = new Ext.ux.tree.TreeGrid({
			enableDD : true,
			columns : [{
						header : '名称',
						dataIndex : 'name',
						width : 200
					}, {
						header : '代码',
						width : 50,
						dataIndex : 'code'
					}, {
						header : '添加',
						width : 50,
						dataIndex: 'add',
						align : 'center'
					}, {
						header : '删除',
						width : 50,
						dataIndex: 'del',
						align : 'center'
					}, {
						header : '修改',
						width : 50,
						dataIndex: 'modify',
						align : 'center'
					}, {
						header : '导入',
						width : 50,
						dataIndex: 'import',
						align : 'center'
					}, {
						header : '导出',
						width : 50,
						dataIndex: 'export',
						align : 'center'
					}],
			tbar : new Ext.Toolbar([{
				text : '确定',
				icon : 'icons/toolBar/add.gif',
				handler : authority
			}]),
			dataUrl : '/menu.action?method=menuAuthorityData&roleIds='+getSelectedRole()
		});
}

function showAuthority(){
	if(authorityMenuGrid){
		authorityMenuGrid.destroy();
	}
	createAuthorityMenuGrid();
	authorityWiin.add(authorityMenuGrid);
	authorityWiin.setTitle('授权');
	authorityWiin.show();
}

function authority(){
	var r = [];
	r = authorityMenuGrid.getChecked();
	var jsonData = '';
	for (var i = 0; i < r.length; i++) {
		var n = r[i];
		var data = n.id+','+Ext.getDom(n.id+'_add_check').checked
				+','+Ext.getDom(n.id+'_del_check').checked
				+','+Ext.getDom(n.id+'_modify_check').checked
				+','+Ext.getDom(n.id+'_import_check').checked
				+','+Ext.getDom(n.id+'_export_check').checked;
		jsonData = jsonData+data;
		if( i+1 < r.length ){
			jsonData = jsonData +':';
		}
	}
	
	postAuthorityData(jsonData,getSelectedRole());
	
	authorityMenuGrid.destroy();
	authorityWiin.removeAll();
}

function postAuthorityData(funData,roleData){
	var roleAuthorityTip = Ext.MessageBox.show({
			msg: '正在提交数据, 请稍后...',
	        progressText: '稍后...',
	        width:300,
	        wait:true,
	        waitConfig: {interval:200}
		});
		
	Ext.Ajax.request({
			url : 'authorize.action?method=authorizeRole',
			params : {'funData' : funData,'roleData' : roleData},
			method : 'POST',
			success : function(response, options) {
				roleAuthorityTip.hide();
				var result = Ext.util.JSON.decode(response.responseText);
				if (result.success) {
					Ext.Msg.alert('提示', '授权成功!');
					authorityWiin.hide();
				}
			},
			failure : function(response, options) {
				roleAuthorityTip.hide();
				Ext.Msg.alert('提示', '授权失败.');
				authorityWiin.hide();
			}
		});
}

function clickAuthority(id){
	Ext.getDom(id+'_main_check').checked = true;
}
